import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'widget/gradient_button.dart';
import 'dart:io';
import 'function.dart';
import 'package:device_info/device_info.dart';
class FeedbackPage extends StatefulWidget {
  final Color mainColor;
  FeedbackPage(this.mainColor);
  @override
  _FeedbackPageState createState() => _FeedbackPageState();
}

class _FeedbackPageState extends State<FeedbackPage> {
  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
  String content;
  String device_model;
  String system_version;

  String validateContent(String value) {
    if (value == "") return "请输入反馈内容";
    return null;
  }
  @override
  initState(){
    super.initState();
    initDeviceInfo();
  }
  initDeviceInfo() async {
    if(Platform.isAndroid){
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      this.device_model=androidInfo.model;
      this.system_version=androidInfo.version.release;
    }else if(Platform.isIOS){
      IosDeviceInfo iosInfo=await deviceInfo.iosInfo;
      this.device_model=iosInfo.model;
      this.system_version=iosInfo.systemVersion;
    }
    setState(() {

    });
  }
  submit() async{
    var data={
      "content":content,
      "system_version":system_version,
      "device_model":device_model,
    };
    var res=await get("https://www.fmtol.com/Home/App/contract",data,null);
    if(res['code']==1){
      toast("谢谢您的反馈");
      Navigator.of(context).pop();
    }else{
      toast("提交失败，请重试");
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new CupertinoNavigationBar(
        actionsForegroundColor: Colors.white,
        backgroundColor: widget.mainColor,
        border: new Border(bottom: BorderSide.none),
        middle: new Text(
          "反馈建议",
          style: new TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.normal,
          ),
        ),

      ),
      body: new Container(
        child: new Form(
          key: _formKey,
          child: Padding(
            padding: const EdgeInsets.all(18.0),
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                new TextFormField(
                  controller: new TextEditingController(),
                  decoration: new InputDecoration(
                    hintText: '请输入反馈内容',
                    labelText: "反馈内容",
                  ),
                  maxLines: 5,
                  onSaved: (value) {
                    content = value;
                  },
                  validator: validateContent,
                ),
                new SizedBox(
                  height: 30.0,
                ),
                new GradientButton(
                  onPressed: () async {
                    final FormState form = _formKey.currentState;
                    if (!form.validate()) {
                    } else {
                      form.save();
                      submit();
                    }
                  },
                  text: "提交反馈",
                  splashColor: widget.mainColor,
                ),
                new SizedBox(
                  height: 10.0,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
